package com.miwzn.boot.modules.sys.web;

import com.miwzn.base.AbstractController;
import com.miwzn.boot.modules.sys.entity.po.SysPermission;
import com.miwzn.boot.modules.sys.entity.qm.SysPermissionQuery;
import com.miwzn.boot.modules.sys.entity.dto.SysPermissionDto;
import com.miwzn.boot.modules.sys.service.SysPermissionService;
import com.miwzn.comm.ResponseEntity;
import com.miwzn.comm.annotation.OperationLog;
import com.miwzn.comm.constant.MyConstant;
import com.miwzn.comm.exception.BsRException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wzn
 * @Date: 2018-12-27 14:43:55
 * @Description: www.miwzn.com mvc api
 */
@ResponseBody
@Controller
@RequestMapping("sys/permission")
public class SysPermissionController extends AbstractController<SysPermission, SysPermissionDto, SysPermissionQuery> {
    private static Logger logger = LoggerFactory.getLogger(SysPermissionController.class);

    @Autowired
    private SysPermissionService sysPermissionService;

    /**
     * 根据ID获取信息
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="根据ID获取权限")
    @RequiresPermissions("sys:permission:getById")
    @GetMapping("/getById/{id}")
    public ResponseEntity getById(@PathVariable("id") String id) {
        return super.getById(id);
    }

    /**
     * 列表
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="获取权限列表")
    @RequiresPermissions("sys:permission:list")
    @GetMapping("/list")
    public ResponseEntity list(SysPermissionQuery entity) {
        return super.listD(entity);
    }

    /**
     * 添加
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="添加权限")
    @RequiresPermissions("sys:permission:add")
    @PostMapping("add")
    public ResponseEntity add(@RequestBody SysPermissionDto entity) {
        checkPermission(entity);
        return super.add(entity);
    }

    /**
     * 更新
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="更新权限")
    @RequiresPermissions("sys:permission:update")
    @PutMapping("update")
    public ResponseEntity update(@RequestBody SysPermissionDto entity) {
        checkPermission(entity);
        updateParentIds(entity);
        return super.update(entity);
    }

    private void checkPermission(SysPermissionDto entity) {
        if (entity.getParentId() == null) {
            entity.setParentId("0");
            entity.setParentIds("0");
        }
        SysPermissionQuery q = new SysPermissionQuery();
        q.setDelFlag(MyConstant.FLAG_N);
        q.setName(entity.getName());
        q.setParentId(entity.getParentId());
        List<SysPermission> sysPermissions = sysPermissionService.select(q);
        //同一父级下不能有相同的NAME
        if (CollectionUtils.isNotEmpty(sysPermissions)) {
            if (entity.getId() == null) throw new BsRException("同一组权限名称不能重复");
            for (SysPermission sysPermission : sysPermissions) {
                if (!sysPermission.getId().equalsIgnoreCase(entity.getId())) {
                    throw new BsRException("同一组权限名称不能重复");
                }
            }
        }
        if (StringUtils.isBlank(entity.getPermissionCode())) return;
        q = new SysPermissionQuery();
        q.setDelFlag(MyConstant.FLAG_N);
        q.setPermissionCode(entity.getPermissionCode());
        q.setParentId(entity.getParentId());
        sysPermissions = sysPermissionService.select(q);
        //同一父级下不能有相同的NAME
        if (CollectionUtils.isNotEmpty(sysPermissions)) {
            if (entity.getId() == null) throw new BsRException("同一组权限码不能重复");
            for (SysPermission sysPermission : sysPermissions) {
                if (!sysPermission.getId().equalsIgnoreCase(entity.getId())) {
                    throw new BsRException("同一组权限码不能重复");
                }
            }
        }

    }

    private void updateParentIds(SysPermissionDto entity) {
        SysPermission sysPermission = sysPermissionService.selectByPrimaryKey(entity.getId());
        if (!sysPermission.getParentId().equalsIgnoreCase(entity.getParentId())) {
            //处理下级的父级
            SysPermission query = new SysPermission();
            query.setParentId(entity.getId());
            List<SysPermission> sysPermissions = sysPermissionService.select(query);
            if (CollectionUtils.isNotEmpty(sysPermissions)) {
                for (SysPermission permission : sysPermissions) {
                    permission.setParentId(entity.getId());
                    permission.setParentIds(entity.getParentIds() + "," + entity.getId());
                    sysPermissionService.updateByPrimaryKeySelective(permission);
                    SysPermissionDto sysPermissionDto = sysPermissionService.wrapD(permission);
                    updateParentIds(sysPermissionDto);
                }
            }
        }
    }

    /**
     * 软删除
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="删除权限")
    @RequiresPermissions("sys:permission:delFlag")
    @DeleteMapping("/delFlag/{id}")
    public ResponseEntity deleteFlag(@PathVariable("id") String id) {
        return super.deleteFlag(id);
    }

    /**
     * 软删除
     *
     * @param ids
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="删除权限")
    @RequiresPermissions("sys:permission:delFlag")
    @DeleteMapping("/delFlag")
    public ResponseEntity deleteFlag(@RequestBody String[] ids) {
        return super.deleteFlag(ids);
    }

    /**
     * 硬删除
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="删除权限")
    @RequiresPermissions("sys:permission:del")
    @DeleteMapping("/del/{id}")
    public ResponseEntity delete(@PathVariable("id") String id) {
        return super.delete(id);
    }

    /**
     * 硬删除
     *
     * @param ids
     *
     * @return
     */
    @OperationLog(operation = "系统-权限",content ="删除权限")
    @RequiresPermissions("sys:permission:del")
    @DeleteMapping("/del")
    public ResponseEntity delete(@RequestBody String[] ids) {
        return super.delete(ids);
    }

}
